﻿@inject ICustomerService CustomerService
@inject ISaleOrderService SaleOrderService
@inject DialogService DialogService

<RadzenStack>
    <RadzenStack>
        <RadzenRow>
            <RadzenColumn>
                <RadzenButton Variant="Variant.Outlined" Click="SubmitHandle">保存</RadzenButton>
                <RadzenButton Variant="Variant.Outlined" Click="CancelHandle">取消</RadzenButton>
            </RadzenColumn>
        </RadzenRow>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1" /><b>基本信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack class="rz-mb-2">
                    <RadzenRow>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="销售单号" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.SaleOrderCode"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="客户名称" Style="width: 100%;">
                                <RadzenDropDown @bind-Value="_editRequestDto.CustomerId" LoadData="LoadDataHandler" Data="_customers"
                                                TextProperty="Name" ValueProperty="Id" AllowFiltering="true">
                                </RadzenDropDown>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="客户单号" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.CustomerOrderCode"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="合同单号" Style="width: 100%;">
                                <RadzenTextBox @bind-Value="_editRequestDto.ContractOrderCode"></RadzenTextBox>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12" SizeSM="6">
                            <RadzenFormField Text="交货日期" Style="width: 100%;">
                                <RadzenDatePicker @bind-Value="_editRequestDto.DeliveryTime"></RadzenDatePicker>
                            </RadzenFormField>
                        </RadzenColumn>
                        <RadzenColumn Size="12">
                            <RadzenFormField Text="描述" Style="width: 100%;">
                                <RadzenTextArea @bind-Value="@_editRequestDto.Description"></RadzenTextArea>
                            </RadzenFormField>
                        </RadzenColumn>
                    </RadzenRow>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
    <RadzenStack>
        <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                        ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
            <HeaderTemplate>
                <span class="d-inline-flex align-items-center align-middle">
                    <RadzenIcon Icon="account_box" class="me-1" /><b>明细信息</b>
                </span>
            </HeaderTemplate>
            <ChildContent>
                <RadzenStack>
                    <RadzenRow>
                        <RadzenColumn>
                            <RadzenButton Variant="Variant.Outlined" Click="AddHandle">Add</RadzenButton>
                        </RadzenColumn>
                    </RadzenRow>
                    <RadzenDataGrid @ref=grid AllowFiltering="true" AllowColumnResize="true" AllowAlternatingRows="false" FilterMode="FilterMode.Advanced" AllowSorting="true" PageSize="5"
                                    AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true"
                                    Data="@_editRequestDto.Items" TItem="SaleOrderDetailEditRequestDto" ColumnWidth="300px" LogicalFilterOperator="LogicalFilterOperator.Or">
                        <Columns>
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="MaterialCode" Title="物料编码" Frozen="true" Width="160px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="MaterialName" Title="物料名称" Frozen="true" Width="160px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="ExcludingTaxPrice" Title="不含税单价" Frozen="true" Width="160px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="IncludingTaxPrice" Title="含税单价" Width="160px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="Tax" Title="税率" Width="200px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="Quantity" Title="数量" Width="120px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Property="DeliveryTime" Title="交货时间" Width="120px" />
                            <RadzenDataGridColumn TItem="SaleOrderDetailEditRequestDto" Filterable="false" Sortable="false" Width="100px" TextAlign="TextAlign.Center">
                                <Template Context="context">
                                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Icon="edit" Size="ButtonSize.Medium"
                                                  Shade="Shade.Lighter" Variant="Variant.Flat"
                                                  Click=@(args => EditHandle(args, context)) @onclick:stopPropagation="true" />
                                    <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="delete" Size="ButtonSize.Medium"
                                                  Shade="Shade.Lighter" Variant="Variant.Flat"
                                                  Click=@(args => DeleteHandler(args, context)) @onclick:stopPropagation="true" />
                                </Template>
                            </RadzenDataGridColumn>
                        </Columns>
                    </RadzenDataGrid>
                </RadzenStack>
            </ChildContent>
        </RadzenFieldset>
    </RadzenStack>
</RadzenStack>

@code
{
    private int _initKey = -1;
    private SaleOrderEditRequestDto _editRequestDto = new();
    private IList<CustomerInfoResponseDto> _customers = new List<CustomerInfoResponseDto>();

    RadzenDataGrid<SaleOrderDetailEditRequestDto> grid;

    [CascadingParameter]
    public SaleOrderIndex? SaleOrderIndex { get; set; }


    async Task AddHandle(MouseEventArgs args)
    {
        SaleOrderDetailEditRequestDto itemData = new()
            {
                Key = _initKey
            };
        var result = await DialogService.OpenAsync<SaleOrderDetailEdit>("添加产品");
        if (result != null)
        {
            _initKey--;
            itemData = (SaleOrderDetailEditRequestDto)result;
            _editRequestDto.Items.Add(itemData);
            await grid.Reload();
        }
    }

    async Task DeleteHandler(MouseEventArgs args, SaleOrderDetailEditRequestDto itemData)
    {
    }

    async Task EditHandle(MouseEventArgs args, SaleOrderDetailEditRequestDto itemData)
    {
        var result = await DialogService.OpenAsync<SaleOrderDetailEdit>("修改产品",
            new Dictionary<string, object>()
                            {
                { "Model",itemData }
                            });
        if (result != null)
        {
            itemData = (SaleOrderDetailEditRequestDto)result;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var result = await CustomerService.QueryCustomers(new CustomerSearchRequestDto());

        if (result != null && result.Succeeded)
        {
            _customers = result.Data;
        }
    }

    async Task LoadDataHandler(LoadDataArgs args)
    {
        var result = await CustomerService.QueryCustomers(new CustomerSearchRequestDto());

        if (result != null && result.Succeeded)
        {
            _customers = result.Data;
        }

        InvokeAsync(StateHasChanged);
    }

    async Task SubmitHandle()
    {
        var key = SaleOrderIndex?.GetKey();

        if (key != null && key > 0)
        {
            var result = await SaleOrderService.UpdateSaleOrder(key.Value, _editRequestDto);
        }
        else
        {
            var result = await SaleOrderService.CreateSaleOrder(_editRequestDto);
        }
    }

    void CancelHandle()
    {
        SaleOrderIndex?.SetKey(0);
        SaleOrderIndex?.SetDynamicComponentType(typeof(SaleOrderTable));
    }
}